<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>示例组件</title>
  <meta name="description" content="">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="../style.css">
</head>

<body>
  <!--[if lt IE 7]>
      <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="#">upgrade your browser</a> to improve your experience.</p>
    <![endif]-->

  <div id="app">
    <p>全局注册组件</p>
    <my-component></my-component>
    <hr>
    <p>局部注册组件</p>
    <my-subcomponent></my-subcomponent>
    <hr>
    <p>表格</p>
    <table>
      <tbody is="my-tablecomp"></tbody>
    </table>
    <hr>
    <p>组件复用,引用相同的数据源</p>
    <my-multicomp></my-multicomp>
    <my-multicomp></my-multicomp>
    <my-multicomp></my-multicomp>
    <hr>
    <p>组件复用,独立数据源</p>
    <my-multicomp2></my-multicomp2>
    <my-multicomp2></my-multicomp2>
    <my-multicomp2></my-multicomp2>
    <hr>
    <p>用props向子组件传递数据</p>
    <my-childcom message="来自父组件的数据"></my-childcom>
    <hr>
    <p>将父组件的动态数据传递给子组件</p>
    <input type="text" v-model="parentMessage">
    <my-childcom2 :message="parentMessage"></my-childcom2>
    <hr>
    <p>注意子组件如果不v-bind,传递的数字、布尔值、数组、对象等数据会被当成字符串</p>
    <span>以下为同一个组件,都传递数组[1,2,3,4],上面的没有使用v-bind，注意长度的不同</s>
    <my-childcom3 message="[1,2,3,4]"></my-childcom3>
    <my-childcom3 :message="[1,2,3,4]"></my-childcom3>
    <hr>
    <p>props传入一个需要转变后使用的原始值</p>
    <my-childcom4 :width="100"></my-childcom4>
    <hr>
    
  </div>

  <script src='https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js'></script>
  <script src="" async defer></script>
  <script>
    var data = {
      counter: 0
    }
    Vue.component('my-component', {
      template: '<div class="component"> 这里是全局注册的组件内容 </div>'
    })

    Vue.component('my-multicomp',{
      template: '<div class="component"><button @click="counter++"> {{counter}} </button></div>',
      data: function () {
        return data
      }
    })

    Vue.component('my-multicomp2', {
      template: '<div class="component"><button @click="counter++"> {{counter}} </button></div>',
      data: function(){
        return {
          counter: 0
        }
      }
    })

    Vue.component('my-childcom', {
      props: ['message'],
      template: '<div class="component">{{ message }}</div>'
    })

    Vue.component('my-childcom2', {
      props: ['message'],
      template: '<div class="component">来自父组件的数据: {{message}}</div>'
    })

    Vue.component('my-childcom3', {
      props: ['message'],
      template: '<div class="component">来自父组件的数据长度: {{message.length}}</div>'
    })

    Vue.component('my-childcom4', {
      props: ['width'],
      template: '<div class="component" :style="style">子组件内容</div>',
      computed:{
        style: function(){
          return {
            width: this.width + 'px'
          }
        }
      }
    })

    var app = new Vue({
      el: '#app',
      data: {
        parentMessage: ''
      },
      components: {
        'my-subcomponent': {
          template: '<div class="component"> 这里是局部注册的组件内容 </div>'
        },
        'my-tablecomp': {
          template: '<tr><td> 这里是表格内的组件内容 </td><td>1</td><td>2</td></tr>'
        }
      }
    })
  </script>
</body>

</html>